let moment = require("moment");

const updateDuration = (time = "") => {
  console.log("更新了", time);
};

let inter;

const handleInterval = () => {
  inter = setInterval(() => {
    // 假如跨度到下一天
    const nextTime = moment().add(10, "s");
    // : moment("2023-01-12 23:59:57").add(3, "s");
    // 凌晨
    const nextDay0h = moment(
      moment().add(1, "m").format("YYYY-MM-DD HH:mm:00")
    );

    // 是否跨度到下一天
    const isNext = moment.max(nextTime, nextDay0h) === nextTime;

    console.log(
      "time",
      isNext,
      moment().format("YYYY-MM-DD HH:mm:ss"),
      nextTime.format("YYYY-MM-DD HH:mm:ss"),
      nextDay0h.format("YYYY-MM-DD HH:mm:ss")
    );
    if (isNext) {
      const timeTmp = nextTime.valueOf() - nextDay0h.valueOf(); // 多少秒之后去更新
      const param = moment("2023-01-12 23:59:59").format("YYYY-MM-DD HH:mm:ss");
      console.log("www====", timeTmp);
      setTimeout(() => {
        updateDuration(param);
        if (inter) {
          clearInterval(inter);
          handleInterval();
        }
      }, timeTmp);
    } else {
      updateDuration();
    }
  }, 1000 * 10);
};

handleInterval();
// console.log("www====inter", inter);
